package com.sniff.atp.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MessageUtil {
    private MessageUtil(){}

    public static void message(String title, Map<String, Object> content) {
        String url = "http://192.168.201.60:22637/job/sniff-atp/allure/";
        String enter = "\n";
        StringBuilder sb = new StringBuilder();
        sb.append("【**").append(title).append("**】").append(enter)
                .append("> - 开始时间：").append(content.get("startDate")).append(enter)
                .append("> - 结束时间：").append(content.get("endDate")).append(enter)
                .append("> - 失败数：").append(content.get("failCount")).append(enter)
                .append("> - 成功数：").append(content.get("passCount")).append(enter)
                .append("> - 跳过数：").append(content.get("skipCount")).append(enter)
                .append("> - 总数：").append(content.get("count")).append(enter)
                .append("> - 通过率：").append(content.get("passRate")).append(" %").append(enter)
                .append("##### 测试报告[查看](").append(url).append(")");
        send(title, sb.toString());
    }

    public static void send(String title, String text) {
        JSONObject json = new JSONObject();
        json.put("title", title);
        json.put("text", text);

        Map<String, Object> at = new HashMap<>();
        at.put("isAtAll", false);

        Map<String, Object> params = new HashMap<>();
        params.put("msgtype", "markdown");
        params.put("markdown", json);
        params.put("at", JSON.toJSON(at));

        HttpClientUtil http = new HttpClientUtil();
        String token = "b27fb31fbfac9b88c529ea32a9f4df3b60e02ac8ed93e44f768cf613958b0c07";
        String url = "https://oapi.dingtalk.com/robot/send?access_token=" + token;
        http.doPostJson(url, params);
    }
}
